package com.css.fxfzxczh.util;


import com.css.fxfzxczh.constants.YNEnum;
import com.css.fxfzxczh.modules.aftershockSequence.entity.RealEarthquakeEntity;
import com.css.fxfzxczh.modules.aftershockSequence.service.SetEarthquakeMapService;
import com.css.fxfzxczh.modules.synthesizeInfoManage.earEveManage.entity.JcStrongCatalog;
import com.css.fxfzxczh.util.HttpClientUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author y1fan
 * @create 2022-09-01 15:56
 */

@Slf4j
@Data
public class Xml2ObjectUtil {

    // 中国的经纬度范围
    final  static double MIN_LAT = 3.8667;
    final static double MAX_LAT = 53.55;
    final static double MIN_LNG = 73.6667;
    final static double MAX_LNG = 135.0667;



    public static List<RealEarthquakeEntity> getRealEarthquakeEntity(String url){
        String s = HttpClientUtil.doPost(url);
      //  System.out.println(s);
        InputStream is = null;
        try {
            SAXReader reader = new SAXReader();
            is = new ByteArrayInputStream(s.getBytes("UTF-8"));
            Document doc = reader.read(is);
            Element root = doc.getRootElement();
            List<Element> article = root.elements("item");
            /*   Element element = article.get(0);*/
            List<RealEarthquakeEntity> list = new ArrayList<>();
            for (Element element : article) {
                RealEarthquakeEntity entity = new RealEarthquakeEntity();
                entity.setFlag(YNEnum.N.toString());
                String cata_id = element.elementText("cata_id");
                String o_time = element.elementText("o_time");
                String m = element.elementText("m");
                String epi_lat = element.elementText("epi_lat");
                String epi_lon = element.elementText("epi_lon");
                if (new BigDecimal(epi_lon).compareTo(BigDecimal.valueOf(73.5)) < 0 || new BigDecimal(epi_lon).compareTo(BigDecimal.valueOf(135.05)) > 0) {
                    //entity.setFlag(YNEnum.Y.toString());
                    continue;
                }
                if (new BigDecimal(epi_lat).compareTo(BigDecimal.valueOf(4)) < 0 || new BigDecimal(epi_lat).compareTo(BigDecimal.valueOf(53.5)) > 0) {
                    //entity.setFlag(YNEnum.Y.toString());
                    continue;
                }
                String epi_depth = element.elementText("epi_depth");
                String auto_flag = element.elementText("auto_flag");
                String location_c = element.elementText("location_c");

                entity.setCataId(cata_id);
                entity.setEarthquakeDate(o_time);
                entity.setMm(m);
                entity.setLatitude(epi_lat);
                entity.setLongitude(epi_lon);
                entity.setEpiDepth(epi_depth);
                entity.setAutoFlag(auto_flag);
                entity.setLocation(location_c);
                list.add(entity);
            }
         /*   System.out.println(article.size());
            System.out.println(realEarthquakeEntity.toString());*/
            return list;
        } catch (DocumentException de) {
            log.error("xml解析失败，页面返回不是xml", de);
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                if (null != is) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    public static List<JcStrongCatalog> getTaiwanNetworkEarthquake(String url,String magnitude) {
        String s = HttpClientUtil.doPost(url);
        //  System.out.println(s);
        InputStream is = null;
        try {
            SAXReader reader = new SAXReader();
            is = new ByteArrayInputStream(s.getBytes("UTF-8"));
            Document doc = reader.read(is);
            Element root = doc.getRootElement();
            List<Element> article = root.elements("item");

            List<JcStrongCatalog> list = new ArrayList<>();
            for (Element element : article) {
                JcStrongCatalog entity = new JcStrongCatalog();
//                entity.setFlag(YNEnum.N.toString());
                String cata_id = element.elementText("cata_id");
                String o_time = element.elementText("o_time");
                String m = element.elementText("m");
                String epi_lat = element.elementText("epi_lat");
                String epi_lon = element.elementText("epi_lon");
                // TODO: 2024/7/11 该判断不够精准，需要后续gis配合
                double lat = Double.parseDouble(epi_lat);
                double lon = Double.parseDouble(epi_lon);
                if (new BigDecimal(epi_lon).compareTo(BigDecimal.valueOf(MIN_LNG)) < 0 || new BigDecimal(epi_lon).compareTo(BigDecimal.valueOf(MAX_LNG)) > 0) {
                    //entity.setFlag(YNEnum.Y.toString());
                    continue;
                }
                if (new BigDecimal(epi_lat).compareTo(BigDecimal.valueOf(MIN_LAT)) < 0 || new BigDecimal(epi_lat).compareTo(BigDecimal.valueOf(MAX_LAT)) > 0) {
                    //entity.setFlag(YNEnum.Y.toString());
                    continue;
                }
                if (PlatformObjectUtils.isNotEmpty(magnitude) && !m.equals(magnitude)){
                    continue;
                }


//                List<String> provinceInfo = setEarthquakeMapService.queryProvinceByLonLat(epi_lon, epi_lat);
//                if (PlatformObjectUtils.isEmpty(provinceInfo)) {
//                    //中国海域
//                    continue;
//                }


                if (new BigDecimal(m).compareTo(new BigDecimal("4.0")) < 0) {
                    continue;
                }
                String epi_depth = element.elementText("epi_depth");
                String auto_flag = element.elementText("auto_flag");
                String location_c = element.elementText("location_c");

                entity.setJscId(cata_id);
                entity.setFzTime(o_time);
                entity.setMagnitude(new BigDecimal(m));
                entity.setLatitude(new BigDecimal(epi_lat));
                entity.setLongitude(new BigDecimal(epi_lon));
                entity.setDepth(Double.parseDouble(epi_depth));

                entity.setLocation(location_c);
                list.add(entity);
            }

            return list;
        } catch (DocumentException de) {
            log.error("xml解析失败，页面返回不是xml", de);
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                if (null != is) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
